﻿using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormsAppToPDF
{
    public partial class FormCert : Form
    {
        public FormCert()
        {
            InitializeComponent();
        }

        private void btnCrePass_Click(object sender, EventArgs e)
        {
            string randomchars = "abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+|0123456789~!@#$%^&*()_+|ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|";
            string password =IniFiles.MakePassword(randomchars, 18);
            txtPassword.Text = password;
        }

        [Obsolete]
        private void btnCreCert_Click(object sender, EventArgs e)
        {
            string password=txtPassword.Text;
            DateTime StartDate=dTPStart.Value;
            DateTime EndDate = dTPEnd.Value;
            // Generate certificate IssuerDN
            var issuerattributes = new Hashtable();
            //issuerattributes[X509Name.E] = "xtiandi@163.com";//设置dn信息的邮箱地址
            issuerattributes[X509Name.CN] = txtissuerCN.Text;//设置证书的用户，也就是颁发给谁
            issuerattributes[X509Name.O] = txtissuerO.Text;//设置证书的办法者
            issuerattributes[X509Name.C] = txtissuerC.Text;//证书的语言
            issuerattributes[X509Name.L] = txtissuerL.Text;
            issuerattributes[X509Name.T] = txtissuerS.Text;
            issuerattributes[X509Name.OU] = txtissuerOU.Text;

            //这里是证书颁发者的信息
            var issuerordering = new ArrayList();
            // issuerordering.Add(X509Name.E);X
            issuerordering.Add(X509Name.CN);
            issuerordering.Add(X509Name.O);
            issuerordering.Add(X509Name.C);
            issuerordering.Add(X509Name.L);
            issuerordering.Add(X509Name.T);
            issuerordering.Add(X509Name.OU);

            // Generate certificate SubjectDN
            var subjectattributes = new Hashtable();
            //subjectattributes[X509Name.E] = "xtiandi@163.com";//设置dn信息的邮箱地址
            subjectattributes[X509Name.CN] = txtsubjectCN.Text;//设置证书的用户，也就是颁发给谁
            subjectattributes[X509Name.O] = txtsubjectO.Text;//设置证书的办法者
            subjectattributes[X509Name.C] = txtsubjectC.Text;//证书的语言
            subjectattributes[X509Name.L] = txtsubjectL.Text;
            subjectattributes[X509Name.T] = txtsubjectS.Text;
            subjectattributes[X509Name.OU] = txtsubjectOU.Text;


            //subjectattributes[X509Name.T] = "shifang";
            //subjectattributes[X509Name.OU] = "Information department";

            //这里是证书颁发者的信息
            var subjectordering = new ArrayList();
            //subjectordering.Add(X509Name.E);
            subjectordering.Add(X509Name.CN);
            subjectordering.Add(X509Name.O);
            subjectordering.Add(X509Name.C);
            subjectordering.Add(X509Name.L);
            subjectordering.Add(X509Name.T);
            subjectordering.Add(X509Name.OU);
            if (sFDialog.ShowDialog() == DialogResult.OK)
            {
                string path=sFDialog.FileName;
                if(Cert.Create(path,new X509Name(issuerordering, issuerattributes), new X509Name(subjectordering, subjectattributes), password,StartDate,EndDate, "SHA256WithRSA"))
                {
                    MessageBox.Show("生成成功","Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("生成失败", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void FormCert_Load(object sender, EventArgs e)
        {
            dTPEnd.Value = DateTime.Today.AddDays(365);
        }
    }
}
